package com.zeek.car90s;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.zeek.car90s.adapter.MapAdapter;
import com.zeek.car90s.util.MediaPlayerUtils;

import java.util.ArrayList;

public class MapActivity extends AppCompatActivity {

    private GridView gridviewMap;
    private ArrayList<Integer> datas;
    private SharedPreferences sp;
    private MapAdapter adapter;
    private Button btnMapBack;
    private int levelCount;//显示的关卡数，小于100显示100，大于100有多少显示多少

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        sp = getSharedPreferences(Constants.SHARED_PREFERENCE_PREFIX, Context.MODE_PRIVATE);
        if(Constants.TOTAL_LEVEL<=100){
            levelCount=100;
        }else {
            levelCount=Constants.TOTAL_LEVEL;
        }

        Constants.MAX_UNLOCK_LEVEL = sp.getInt(Constants.MAX_LEVEL_PREFIX, 0);
        if(Constants.IS_UNLOCK_ALL_LEVEL) {
            Constants.MAX_UNLOCK_LEVEL = levelCount-1;
        }
        datas = new ArrayList<>();
        gridviewMap = (GridView) findViewById(R.id.gridview_map);
        gridlist();
    }

    public void gridlist() {
//        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
//                R.layout.map_gridview_item, new String[]{"textview"},
//                new int[]{R.id.gridviewtextview});
//        gridviewMap.setAdapter(simpleAdapter);

        for (int i = 0; i < levelCount; i++) {
            datas.add(i);
        }
        adapter = new MapAdapter(datas, this);
        gridviewMap.setAdapter(adapter);
        gridviewMap.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MediaPlayerUtils.playNotification(1);
                Constants.CURRENT_LEVEL = position;
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt(Constants.CURRENT_LEVEL_PREFIX, Constants.CURRENT_LEVEL);
                editor.commit();

                if (position <= Constants.MAX_UNLOCK_LEVEL) {
                    startActivity(new Intent(MapActivity.this, GameActivity.class));
                } else {
                    Toast.makeText(MapActivity.this, R.string.unlock_hint, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
